package dapps.web.user;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.web.struts2.CRUDActionSupport;

import dapps.entity.user.District;
import dapps.entity.user.Role;
import dapps.entity.user.Type;
import dapps.entity.user.User;
import dapps.service.ServiceException;
import dapps.service.monitor.ElementManager;
import dapps.service.user.UserManager;
import dapps.service.user.UserjdbcManager;

@SuppressWarnings("serial")
@Results( { @Result(name = CRUDActionSupport.RELOAD, location = "company.action?page.pageRequest=${page.pageRequest}", type = "redirect")})
public class CompanyAction extends CRUDActionSupport<User> {

	@Autowired
	private UserManager userManager;
	@Autowired
	private ElementManager eManager;
	@Autowired
	private UserjdbcManager userjdbcManager;
	// 基本属性
	private User entity;
	private Long id;
	private Page<User> page = new Page<User>(15);//每页5条记录
	private String url;  
	private Long typeid;
	private Long districtid;
	private String companyname;
	private String companyusername;
	private Integer companycode; 
	User u = SpringSecurityUtils.getCurrentUser();
	Long udid = u.getDistrict().getId();
	// 角色相关属性

	private List<Type> typelist;
	private List<District> districtlist;
	// 基本属性访问函数 //

	public User getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = userManager.get(id);
			typeid = entity.getType().getId();
			districtid = entity.getDistrict().getId();
		} else {
			entity = new User();
			Role r= new Role();
			r.setId(3L);//company
			entity.getRoles().add(r);
			Type t = new Type();
			t.setId(typeid);
			District d = new District();
			d.setId(districtid);
			entity.setType(t);
			entity.setDistrict(d);
		}
	}
	public String getUrl() {  
	     return url;  
	}  
	public void setId(Long id) {
		this.id = id;
	}

	public Page<User> getPage() {
		return page;
	}

	// CRUD Action 函数 //

	public Long getTypeid() {
		return typeid;
	}

	public void setTypeid(Long typeid) {
		this.typeid = typeid;
	}

	public Long getDistrictid() {
		return districtid;
	}

	public void setDistrictid(Long districtid) {
		this.districtid = districtid;
	}

	@Override
	public String list() throws Exception {
		
		if(udid!=null&&udid>1L) {
			districtid = udid;
		}
		page = userManager.getCompanypage(page, districtid, companyname,companyusername,companycode);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		typelist = eManager.getTypelist(new Criterion[]{Restrictions.ne("tid", new Integer(0))},null, new Order[] {Order.asc("tid")});
		districtlist = userManager.getDistrictlist((udid!=null&&udid>1L)?new Criterion[]{Restrictions.eq("id", udid)}:null,null, null);
		return INPUT;
	}

	@Override
	public String save() throws Exception {

		userManager.save(entity);			
		userjdbcManager.save(entity);
		addActionMessage("保存企业成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		try {
			userManager.delete(id);
			userjdbcManager.delUser(id);
			addActionMessage("删除企业成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}

	// 其他属性访问函数与Action函数 //



	public List<Type> getTypelist() {
		return typelist;
	}

	public List<District> getDistrictlist() {
		return districtlist;
	}



	public String getCompanyname() {
		return companyname;
	}

	public void setCompanyname(String companyname) {
		this.companyname = companyname;
	}

	public String getCompanyusername() {
		return companyusername;
	}

	public void setCompanyusername(String companyusername) {
		this.companyusername = companyusername;
	}

	public Integer getCompanycode() {
		return companycode;
	}

	public void setCompanycode(Integer companycode) {
		this.companycode = companycode;
	}




}
